﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NLog;
using WCell.Core.DBC;
using WCell.MPQTool;
using WCell.RealmServer;

namespace WCell.Tools.Maps
{
    public static class DBCMapReader
    {
        private static Logger log = LogManager.GetCurrentClassLogger();

        public static List<DBCMapEntry> GetMapEntries()
        {
            var wowRootDir = DBCTool.FindWowDir(null);
            if (!Directory.Exists(wowRootDir))
            {
                log.Error("Could not find WoW-directory: \"{0}\"", wowRootDir);
                return null;
            }

            var DBCMapEntryReader = new DBCReader<DBCMapEntry, DBCMapConverter>(
                RealmServerConfiguration.GetDBCFile("Map.dbc"));

            return DBCMapEntryReader.EntryList;
        }
    }

    public class DBCMapEntry
    {
        public uint Id;
        public string MapDirName;
    }

    public class DBCMapConverter : DBCRecordConverter<DBCMapEntry>
    {
        public override DBCMapEntry ConvertTo(byte[] rawData, ref int id)
        {
            uint i = 0;
            var mapEntry = new DBCMapEntry();
            id = (int)(mapEntry.Id = GetUInt32(rawData, i++)); // 0
            mapEntry.MapDirName = GetString(rawData, i++); // 1

            return mapEntry;
        }
    }


}
